package lgMore;

import static robocode.util.Utils.normalRelativeAngleDegrees;

import java.awt.Color;

import robocode.HitByBulletEvent;
import robocode.HitRobotEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;

public class LgmoreWalls extends Robot {

	boolean peek; // Don't turn if there's a robot there
	double moveAmount; // How much to move
	int cantMove;
	double gunTurnAmt; // How much to turn our gun when searching
	String trackName; // Name of the robot we're currently tracking
	int count = 0; // Keeps track of how long we've
	int contadorBullets;
	/**
	 * run: Move around the walls
	 */
	public void run() {
		// Set colors
		setBodyColor(Color.black);
		setGunColor(Color.black);
		setRadarColor(Color.orange);
		setBulletColor(Color.cyan);
		setScanColor(Color.cyan);

		// Initialize moveAmount to the maximum possible for this battlefield.
		moveAmount = Math.max(getBattleFieldWidth(), getBattleFieldHeight());
		// Initialize peek to false
		peek = false;
		// Prepare gun
		trackName = null; // Initialize to not tracking anyone
		setAdjustGunForRobotTurn(true); // Keep the gun still when we turn
		gunTurnAmt = 10; // Initialize gunTurn to 10
		// turnLeft to face a wall.
		// getHeading() % 90 means the remainder of
		// getHeading() divided by 90.
		turnLeft(getHeading() % 90);
		ahead(moveAmount);
		// Turn the gun to turn right 90 degrees.
		peek = true;
		turnGunRight(90);
		turnRight(90);

		while (true) {
			if (contadorBullets>=5) {
				turnLeft(45);
				ahead(moveAmount/4);
				contadorBullets=0;
			}
			if (getHeading()%90!=0){
				turnLeft(getHeading()%90);
				ahead(moveAmount);
			}
					
			// Look before we turn when ahead() completes.
			peek = true;
			// Move up the wall
			ahead(moveAmount);
			// Don't look now
			peek = false;
			// Turn to the next wall
			turnRight(90);
			//if(getRadarHeading()>180 && getX()<getBattleFieldHeight()/2){
			//turnRadarRight(45);
			//}
			// turn the Gun (looks for enemy)
			turnGunRight(gunTurnAmt);
			// Keep track of how long we've been looking
			count++;
			// If we've haven't seen our target for 2 turns, look left
			if (count > 2) {
				gunTurnAmt = -10;
			}
			// If we still haven't seen our target for 5 turns, look right
			if (count > 3) {
				gunTurnAmt = 10;
			}
			// If we *still* haven't seen our target after 10 turns, find another target
			if (count > 5) {
				trackName = null;
			}
			if (count > 100){
				turnRadarRight(90);
				turnGunRight(90);
				
				count=0;
			}
			
			
		}
	}

	/**
	 * onHitRobot:  Move away a bit.
	 */
	public void onHitRobot(HitRobotEvent e) {
		contadorBullets++;
		double absoluteBearing = getHeading() + e.getBearing();
		double bearingFromGun = normalRelativeAngle(absoluteBearing - getGunHeading());

		// If it's close enough, fire!
		if (Math.abs(bearingFromGun) <= 3) {
			turnGunRight(bearingFromGun);
			// We check gun heat here, because calling fire() 
			// uses a turn, which could cause us to lose track
			// of the other robot.
			if (getGunHeat() == 0) {
				fire(Math.min(3 - Math.abs(bearingFromGun), getEnergy() - .1));
			}
		} // otherwise just set the gun to turn.
		// Note:  This will have no effect until we call scan()
		else {
			turnGunRight(bearingFromGun);
		}

		//if (peek) {
		//	scan();
		//}
		
		// If he's in front of us, set back up a bit.
		if (e.getBearing() > -90 && e.getBearing() < 90) {
						
			back(100);
			
		} // else he's in back of us, so set ahead a bit.
		else {
			ahead(100);
		}
		turnLeft(90 - e.getBearing());
		if (peek) {
			scan();
		}
		
	}

	/**
	 * onScannedRobot:  Fire!
	 */
	public void onScannedRobot(ScannedRobotEvent e) {
		// If we have a target, and this isn't it, return immediately
				// so we can get more ScannedRobotEvents.
				//if (trackName != null && !e.getName().equals(trackName)) {
				//	return;
				//}

				// If we don't have a target, well, now we do!
				//if (trackName == null) {
				//	trackName = e.getName();
				//	out.println("Tracking " + trackName);
				//}
				// This is our target.  Reset count (see the run method)
				count = 0;
				// If our target is too far away, turn and move toward it.
				if (e.getDistance() > 100 && e.getDistance() < 500) {
					gunTurnAmt = normalRelativeAngleDegrees(e.getBearing() + (getHeading() - getRadarHeading()));

					turnGunRight(gunTurnAmt); // Try changing these to setTurnGunRight,
					fire(3);
					//turnRight(e.getBearing()); // and see how much Tracker improves...
					// (you'll have to make Tracker an AdvancedRobot)
					//ahead(e.getDistance() - 140);
					return;
				}
				if (e.getDistance()<700){
					// Our target is close.
					gunTurnAmt = normalRelativeAngleDegrees(e.getBearing() + (getHeading() - getRadarHeading()));
					
					turnGunRight(gunTurnAmt);
					fire(1);
					return;
				}
				// Our target is too close!  Back up.
				if (e.getDistance() < 100) {
					if (e.getBearing() > -90 && e.getBearing() <= 90) {
						back(40);
					} else {
						ahead(40);
					}

				}
				scan();
	}
	public void onHitByBullet(HitByBulletEvent e) {
			
			ahead(100);
			turnLeft(90 - e.getBearing());
			//turnRadarRight(45);
			
			// Our target is close.

			//turnRight(e.getBearing()); // and see how much Tracker improves...
			// (you'll have to make Tracker an AdvancedRobot)
			//ahead(e.getDistance() - 140);
			return;





			
	}
	
	// Helper function
	public double normalRelativeAngle(double angle) {
		if (angle > -180 && angle <= 180) {
			return angle;
		}
		double fixedAngle = angle;

		while (fixedAngle <= -180) {
			fixedAngle += 360;
		}
		while (fixedAngle > 180) {
			fixedAngle -= 360;
		}
		return fixedAngle;
	}
	
}
